package com.lungle.warmdiary.app.view.wallet.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseActivity;
import com.lungle.warmdiary.app.base.BaseFragment;
import com.lungle.warmdiary.app.base.BaseSubscriber;
import com.lungle.warmdiary.app.model.Result;
import com.lungle.warmdiary.app.model.response.AlipayAccount;
import com.lungle.warmdiary.app.model.response.Wallet;
import com.lungle.warmdiary.app.utils.UIHelper;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class UserWalletFragment extends BaseFragment {
    private static final String USER_WALLET_SCREEN = "user wallet screen";
    @Bind(R.id.tv_wallet_balance)
    TextView mTvBalance;
    @Bind(R.id.ly_wallet_record)
    LinearLayout mLyRecord;
    @Bind(R.id.ly_takecash)
    LinearLayout mLyTakecash;
    @Bind(R.id.ly_take_account)
    LinearLayout mLyAlipaySetting;


    private LinearLayout mWalletRecord;
    private LinearLayout mTakeCash;
    private LinearLayout mTackAccount;


    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wallet, container,
                false);

        initData();
        initViews(view);

        return view;
    }

    private void initViews(View view) {
        ButterKnife.bind(this, view);
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).setActionBarTitle("");
        }
    }

    @Override
    public void initData() {

    }


    @Override
    public void onResume() {
        super.onResume();
        getWalletInfo();
        MobclickAgent.onPageStart(USER_WALLET_SCREEN);
        MobclickAgent.onResume(getActivity());

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(USER_WALLET_SCREEN);
        MobclickAgent.onPause(getActivity());

    }


    private void getWalletInfo() {
        showWaitDialog();
        ApiFactory.getUserApi().getWallet().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<Result<Wallet>>(getActivity()) {
                    @Override
                    public void onNext(Result<Wallet> walletResult) {
                        super.onNext(walletResult);
                        mTvBalance.setText(walletResult.getData().getIncome() + "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        hideWaitDialog();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        hideWaitDialog();
                    }
                });
    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.ly_wallet_record, R.id.ly_takecash, R.id.ly_take_account})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_wallet_record:
                UIHelper.showTradeRecord(getActivity());
                break;
            case R.id.ly_takecash:
                UIHelper.showWithdrawRecord(getActivity());
                break;
            case R.id.ly_take_account:
                hasBindAlipay();

                break;
        }
    }

    private void hasBindAlipay() {
        showWaitDialog();
        ApiFactory.getUserApi().getAliPay().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<Result<AlipayAccount>>(getActivity()) {
                    @Override
                    public void onNext(Result<AlipayAccount> alipayAccountResult) {
                        super.onNext(alipayAccountResult);
                        AlipayAccount mAccount = alipayAccountResult.getData();
                        if (!TextUtils.isEmpty(mAccount.getAccount())) {
                            UIHelper.showAccountCenter(getActivity(), mAccount);
                        } else {
                            UIHelper.showAccountCenter(getActivity(), null);
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        hideWaitDialog();
                        AppContext.showToast("请重试");


                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        hideWaitDialog();
                    }
                });
    }
}
